package com.genius.service.impl;

import com.genius.enums.ResponseType;
import com.genius.exception.GraceException;
import com.genius.mapper.MenuMapper;
import com.genius.service.MenuService;
import com.genius.system.pojo.Menu;
import com.genius.system.pojo.User;
import com.genius.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> selectAllMenu(Menu menu) {
        List<Menu> menus = menuMapper.selectAllMenu(menu);
        if(menus.isEmpty()){
            return new ArrayList<>();
        }
        return buildMenus(menus);
    }

    @Override
    public boolean selectMenuIsExistByMenuIds(Long[] menuIds) {
        return menuMapper.selectMenuIsExistByMenuIds(menuIds);
    }

    @Override
    @Transactional
    public int insertMenu(Menu menu) {

        checkMenuIsExist(menu);
        return menuMapper.insertMenu(menu);
    }

    @Override
    @Transactional
    public int updateMenu(Menu menu){

        checkMenuIsExist(menu);
        return menuMapper.updateMenu(menu);
    }

    @Override
    @Transactional
    public int deleteMenuByIds(Long[] ids){
        if (!menuMapper.menuCanDeleteOrNot(ids)) {
            GraceException.display(ResponseType.CHILD_NODE_EXIST_ERROR);
        }
        return menuMapper.deleteMenuByIds(ids);
    }

    @Override
    public List<Menu> selectMenuByUserId(HashMap<String,Object> hashMap) {
        List<Menu> menus = new ArrayList<>(menuMapper.selectMenuByUserId(hashMap));
        if(menus.isEmpty()){
            return new ArrayList<>();
        }
        return buildMenus(menus);
    }

    @Override
    public List<String> selectMenuByRoleId(Long roleId) {
        return menuMapper.selectMenuByRoleId(roleId);
    }

    @Override
    public Set<String> getPermission(User user) {
        return menuMapper.selectPermissionByUserId(user.getUserId());
    }

    /**
     * 构建菜单树形结构
     * @param menuList 菜单列表
     * @return 树形结构菜单列表
     */
    private List<Menu> buildMenus(List<Menu> menuList){
        List<Menu> menuTree = new ArrayList<>();
        try{
            menuTree.addAll(TreeUtils.buildTree(menuList, "menuId"));
        }catch(Exception e){
            e.printStackTrace();
        }
        return menuTree;
    }

    /**
     * 检查菜单是否已存在
     * @param menu 菜单信息
     */
    private void checkMenuIsExist(Menu menu) {
        HashMap<String,Object> hashMap = new HashMap<>();
        hashMap.put("menuName",menu.getMenuName());
        hashMap.put("parentId",menu.getParentId());
        hashMap.put("menuId",menu.getMenuId());
        if (!menuMapper.selectMenuIsExistByMenuName(hashMap)) {
            GraceException.display(ResponseType.MENU_EXIST_ERROR);
        }
    }
}
